package com.edu.framework.websocket.handler;

import com.edu.framework.websocket.WebSocketHolder;
import com.edu.framework.websocket.message.base.Response;
import com.edu.framework.websocket.message.terminal.TerminalRequest;
import com.edu.framework.websocket.message.terminal.TerminalVO;
import com.edu.framework.websocket.service.BaseInfoService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.socket.WebSocketSession;

import java.io.IOException;
import java.util.Map;

@Component
public class TerminalRequestHandler implements IHandler<TerminalRequest> {

    @Autowired
    private BaseInfoService baseInfoService;

    @Override
    public void execute(WebSocketSession session, TerminalRequest message) {
        if (message == null) {
            return;
        }
        Map<String, TerminalVO> data = message.getData();
        TerminalVO info = data.get("info");
        Response response = baseInfoService.regist(info);
        try {
            WebSocketHolder.send(session,response);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }


    @Override
    public String getType() {
        return TerminalRequest.TYPE;
    }
}
